function getWxDeptTreeName(depts,deptId){
	var name = "";
	var dept = getWxDeptById(depts, deptId);
	if(dept.parentid != 0){
		name = getWxDeptTreeName(depts, dept.parentid) + " > " + dept.name;
		
	}else{
		name = dept.name;
	}
	return name;
}

function WechatHelper(ctxPath,prefix,app){
	
	var _this = this;
	
	this.localIds;
	this.images;
	
	this._init = function(){
		io.socket.get(ctxPath + prefix + '/wechat/jsticket', {}, function(resData, jwres){
			if(jwres.statusCode == 200 && resData.success){
				var ticket = resData.ticket;
				var noncestr = getRandomString(16);
				var timestamp = new Date().getTime();
				var signsrc = "jsapi_ticket=" + ticket + "&noncestr=" + noncestr + "&timestamp=" + timestamp + "&url=" + ctxPath + prefix;
				var signature = SHA1(signsrc);
				
				wx.config({ 
					debug: false,
					appId: resData.appId,
					timestamp: timestamp,
					nonceStr: noncestr,
					signature: signature,
					jsApiList: ["chooseImage","previewImage","uploadImage"] 
				});
				
				wx.hideOptionMenu();
			}else{
				app.alert(resData.message);
			}
		});
	};
	
	this.chooseImages = function(picker){
		this.images = new Array();
		wx.chooseImage({
			success: function (res) {
				_this.localIds = res.localIds;
				if(_this.localIds.length > 3){
					_this.localIds.length = 3;
				}
				picker.find("a img").removeAttr("src");
				for (var i = 0; i < _this.localIds.length; i++) {
					picker.find("a:eq("+i+") img").attr("src",_this.localIds[i]);
				}
			}
		});
	};
	
	this.uploadImages = function(callback){
		if(this.localIds != null && this.localIds.length > 0){
			var nlocalId =  this.localIds.shift();
			wx.uploadImage({
				localId: nlocalId,
				isShowProgressTips: 1,
				success: function (res1) {
					var mediaId = res1.serverId;
					_this.images.push(mediaId);
					
					if(_this.localIds.length == 0){
						callback(_this.images);
					}else{
						_this.uploadImages(callback);
					}
				}
			});
		}else{
			callback();
		}
	};
	
	this.previewImage = function(){
		$(".wx-gallery-view a").on('click', function(e){
			var gallery = $(this).closest(".wx-gallery-view");
			var current = $(this).find("img");
			var imgs = gallery.find("img");
			if(imgs.length > 0){
				var urls = new Array();
				imgs.each(function(){
					urls.push($(this).attr("src"));
				});
				wx.previewImage({
					current: current.attr("src"),
					urls: urls
				});
			}
		});
	}
	
	this._init();
	
}